package com.offcn.web;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.Course;
import com.offcn.service.CourseService;
import com.offcn.service.CourseServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.FileUploadUtils;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends BaseServlet {
    private CourseService courseService = new CourseServiceImpl();
     ObjectMapper objectMapper = new ObjectMapper();
     ResultVo resultVo ;
    //     上传文件和视频
    public void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            String fileName = FileUploadUtils.fileUpload(req);
//            判断文件名是否是图片后缀
            if (fileName.contains(".jpg") || fileName.contains(".png")
                    || fileName.contains(".bmp") || fileName.contains(".gif")){
                resultVo = new ResultVo(200,"图片上传成功",fileName);
            }else {
                resultVo = new ResultVo(201,"视频上传成功",fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
//            报错就是上传失败
            resultVo = new ResultVo(500,"文件上传失败",null);
        }  finally {
            resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
        }



    }
    //    删除文件和视频
    public void removeFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取文件名字
        String fileName = req.getParameter("fileName");
//        文件的路径拼上文件名
        File file = new File("C:\\Users\\Administrator\\Desktop\\upload\\",fileName);
//        调用方法进行删除
         file.delete();

    }
     //    添加课程

   public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
       Course course = new Course();
       BeanUtils.populate(course,req.getParameterMap());
       boolean b = courseService.add(course);
       if (b){
           resultVo = new ResultVo(200,"课程添加成功",null);
       }else {
           resultVo = new ResultVo(500,"课程添加失败",null);
       }
       resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
   }

    //分页查询
    public  void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String  search = req.getParameter("search");
        int  currentPage =Integer.parseInt(req.getParameter("currentPage")) ;
        int  pageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageUtils pageUtils = courseService.find(search,currentPage,pageSize);
        resultVo = new ResultVo(200,"查询成功",pageUtils);
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

    //修改课程
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
           Course course = new Course();
           BeanUtils.populate(course,req.getParameterMap());
           boolean b = courseService.update(course);
           if (b){
               resultVo = new ResultVo(200,"课程修改成功",null);
           }else {
               resultVo = new ResultVo(500,"课程修改失败",null);
           }
           resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

    //删除数据
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] cids = req.getParameterValues("cid");
        String[] courseImages = req.getParameterValues("courseImage");
        File file = null;
        for (String courseImage : courseImages) {
            file = new File("C:\\Users\\Administrator\\Desktop\\upload\\",courseImage);
            file.delete();
        }
        boolean b =  courseService.del(cids);
        if (b){
            resultVo = new ResultVo(200,"删除课程成功",null);
        }else {
            resultVo = new ResultVo(500,"删除课程失败",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }
}
